package com.delivery.deliverysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delivery.deliverysystem.pojo.DAO.ProductDAO;
import com.delivery.deliverysystem.pojo.DO.Product;
import com.delivery.deliverysystem.pojo.DO.ProductCategory;
import com.delivery.deliverysystem.pojo.VO.ProductNameVO;
import com.delivery.deliverysystem.pojo.VO.ProductVO;
import com.delivery.deliverysystem.service.ProductService;
import com.delivery.deliverysystem.dao.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.delivery.deliverysystem.common.ServerResponse;

/**
* @author Administrator
* @description 针对表【product(产品资料表)】的数据库操作Service实现
* @createDate 2025-01-13 15:37:40
*/
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Value("${file.upload-path}")
    private String path;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Object createProdct(MultipartFile file, ProductDAO productDAO) {
        Product pro = productMapper.selectById(productDAO.getProductId());
        if (pro != null) {
            return ServerResponse.createByError("产品代码重复");
        }
        Product product = new Product();
        BeanUtils.copyProperties(productDAO, product);
        if (file != null && !file.isEmpty()){
            try{
                //暂时存在本地，到时候存上oss
                String fileName = file.getOriginalFilename();
                // 保存文件
                File destFile = new File(path + fileName);
                file.transferTo(destFile);
                product.setPictureUrl(path+fileName);
            } catch (IOException e) {
                e.printStackTrace();
                return ServerResponse.createByError("文件上传失败");
            }
        }
        productMapper.insert(product);
        return ServerResponse.createBySuccess(product);
    }

    @Override
    public Object updateProdct(MultipartFile file, ProductDAO productDAO) {
        Product product = productMapper.selectById(productDAO.getProductId());
        if (product == null) {
            return ServerResponse.createByError("产品不存在");
        }
        BeanUtils.copyProperties(productDAO, product);
        if (file != null && !file.isEmpty()){
            try{
                //暂时存在本地，到时候存上oss
                String fileName = file.getOriginalFilename();
                // 保存文件
                File destFile = new File(path + fileName);
                file.transferTo(destFile);
                product.setPictureUrl(path+fileName);
            } catch (IOException e) {
                e.printStackTrace();
                return ServerResponse.createByError("文件上传失败");
            }
        }
        // 更新产品信息到数据库
        boolean result = productMapper.updateById(product) > 0;
        if (result) {
            return ServerResponse.createBySuccess(product);
        } else {
            return ServerResponse.createByError("更新失败");
        }
    }

    @Override
    public Product getProductById(String id) {
        if(id == null)return null;
        Product product = productMapper.selectById(id);
        if(product != null){
            return Product.builder()
                    .productId(product.getProductId())
                    .code(product.getCode())
                    .productName(product.getProductName())
                    .number(product.getNumber())
                    .color(product.getColor())
                    .unit(product.getUnit())
                    .singleWeight(product.getSingleWeight())
                    .category(product.getCategory())
                    .price(product.getPrice())
                    .status(product.getStatus())
                    .cost(product.getCost())
                    .remark(product.getRemark())
                    .pictureUrl(product.getPictureUrl())
                    .build();
        }
        return null;
    }

    @Override
    public List<ProductVO> getProductList() {
        List<Product> products = productMapper.selectList(null);
        if(products!=null && !products.isEmpty()){
            return products.stream()
                    .map(product -> ProductVO.builder()
                            .productId(product.getProductId())
                            .productName(product.getProductName())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<ProductNameVO> getProductStatusList() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<Product> products = productMapper.selectList(queryWrapper);
        if(products!=null && !products.isEmpty()){
            return products.stream()
                    .map(product -> ProductNameVO.builder()
                            .productId(product.getProductId())
                            .productName(product.getProductName())
                            .number(product.getNumber())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }
}




